<?php
	// Check crossing acess
	if (!defined('RIGHT_ACCESS') || !defined('ROOT_FOLDER'))
	{
		header('Content-Type: text/xml; charset=utf-8');
		echo '<?xml version="1.0"?>';
		echo '<atomtask>';
		echo '<request>error.request.invalid</request>';
		echo '</atomtask>';
		return;
	}	
	include_once(ROOT_FOLDER.'db/connect_db.php');
	class Project
	{
		public $project_id;
		public $project_name;
		public $project_description;
		public $create_time;
		public $creator_id;
		public $members;
		function __construct()
		{

		}
		public function create()
		{
			if ($this->creator_id == '')
				return false;
			if ($this->project_name == '')
				return false;
			if ($this->project_description == '')
				return false;
			// First, insert into project table
			$query_string = 'INSERT INTO `project`(`project_name`, `project_description`, `creator_id`) VALUES ("'.$this->project_name.'", "'.$this->project_description.'", "'.$this->creator_id.'")';	
			$result = DB::query($query_string);
			if (!$result)
				return false;

			// Next, insert admin into members list
			$this->project_id = mysql_insert_id();
			$query_string = 'INSERT INTO `project_members`(`project_id`, `member_id`, `wait`, `join_date`) VALUES ("'.$this->project_id.'", "'.$this->creator_id.'", "0", CURRENT_DATE())';
			$result = DB::query($query_string);
			if (!$result)
				return false;
			return true;
		}
		public function get()
		{
		}
		public function remove()
		{
		}
		public function update()
		{
		}
		public function invite_member($user_id)
		{
		}
		public function remove_member($user_id)
		{
		}
		public static function get_user_projects($user_id)
		{
			$query_string = 'SELECT  `project`.`project_id`, `project`.`project_name` ,  `project`.`project_description`, `project_members`.`wait` , `admin_table`.`username` as `creator`, `member_table`.`username` as `member`, `project_members`.`join_date`'
		    . 'FROM `project` '
		    . 'LEFT JOIN `project_members`'
		    . 'ON ( `project`.`project_id` =  `project_members`.`project_id`)'
		    . 'LEFT JOIN `user` as `admin_table`'
		    . 'ON ( `project`.`creator_id` = `admin_table`.`user_id`)'
		    . 'LEFT JOIN `user` as `member_table`'
		    . 'ON ( `project_members`.`member_id` = `member_table`.`user_id`)'
		    . 'WHERE `member_table`.`user_id` = "'.$user_id.'"';
		    $result = DB::query($query_string);
		    return $result;
		}
	}
?>